package com.ruoyi.system.controller;

import com.ruoyi.system.domain.Camera_Model;
import com.ruoyi.system.mapper.Camera_ModelMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Time;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/camera_model")
public class Camera_ModelController {
    @Resource
    Camera_ModelMapper camera_modelMapper;
    //摄像头配置模型
    //camera_model
    //开启服务时状态置为1，关闭时置为0
    @PutMapping()
    //用于添加关联，修改关联和修改状态
    //key不可以为空
    //key为0表示修改状态或新建，此时需传入空的json传
    //key为1表示修改关联信息，model_id和camera_id可以为空
    //如果有问题直接问我
    public String update(@RequestParam String model_id,
                         @RequestParam String camera_id,
                         @RequestParam Integer key,
                         @RequestBody Camera_Model cameraModel){
        if(key==0){
            //修改状态或新建
            List<Camera_Model> list = camera_modelMapper.findAll();
            int temp = 0;
            for (Camera_Model camera_model : list){
                if (camera_model.getCamera_id()==Integer.valueOf(camera_id)&&camera_model.getModel_id().equals(model_id)){
                    temp = 1;
                    int k = 0;
                    if (camera_model.getState()==0){
                        k=1;
                    }
                    camera_modelMapper.changeState(camera_model.getId(),k);
                    return "change_state_successful";
                }
            }
            if(temp == 0){
                //新建
                Integer state = 1;//默认开启
                LocalDateTime Dt= LocalDateTime.now();
                DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyMMddHHmmss");
                String new_id = "cam_mod"+Dt.format(dtf);
                Camera_Model camera_model = new Camera_Model();
                camera_model.setId(new_id);
                camera_model.setModel_id(model_id);
                camera_model.setCamera_id(Integer.valueOf(camera_id));
                camera_model.setState(state);
                camera_model.setStart_time(Time.valueOf("00:00:00"));
                camera_model.setEnd_time(Time.valueOf("00:00:00"));
                camera_model.setTime_span(0);
                camera_model.setVoice(0);
                camera_model.setPlayback_times(0);
                camera_model.setMonday(0);
                camera_model.setTuesday(0);
                camera_model.setWednesday(0);
                camera_model.setThursday(0);
                camera_model.setFriday(0);
                camera_model.setSaturday(0);
                camera_model.setSunday(0);
                camera_modelMapper.add(camera_model);
                return "new_successful";
            }
        }else if(key==1){
            //更新参数
            if(camera_modelMapper.upadte(cameraModel)){
                return "update_successful";
            }
        }

        return "false";
    }


    @GetMapping
    //根据id获取摄像头-模型关联
    public Camera_Model findById(@RequestParam String id){
        return camera_modelMapper.findById(id);
    }
}
